<?php 
namespace Admin\Controller;

class NewsController extends BaseController{
	function add(){
		$optionOb= M('article');

		$this->display();
	}
	function save(){
		//保存图片
// 	实例化new \Think\Upload()
		$uploadOb=new \Think\Upload();
// 	设置 __set()
		$uploadOb->mimes=array('image/png',"image/gif","image/jpeg","image/jpg");
		$uploadOb->autoSub=false;
		$uploadOb->rootPath="./Public/Uploads/";
		
// 	调用upload()保存图片
		$imageRe=$uploadOb->upload();
	
			if($imageRe){
			//水印 缩略图
			
			$_POST['imagename']=$imageRe[0]['savename'];
			}else{
			//var_dump($uploadOb->getError());
			//EXIT();
			$this->error('图片上传失败',U('Admin/News/add'));
			}
		$_POST['editorValue']=isset($_POST['editorValue'])?$_POST['editorValue']:'未添加文章内容';
		$type = $_POST['type'];
		$type = explode('|',$type);
		$_POST['typefid'] = $type[0];
		$_POST['typeid'] = $type[1];
		unset($_POST['type']);
		$newsOb =M('article');
		// $cre = $newsOb->create();
		// if ($cre) {
		// 	# code...
		// 	$re= $newsOb->add();
		// 	if($re){
		// 	$this->success('添加成功',U('Admin/News/oper'));
		// 	}else{
		// 	$this->error('添加失败',U('Admin/News/add'));
		// 	}
		// }else{
		// 	$info= $newsOb->getError();
		// 	$this->error($info,U('Admin/News/add'));
		// }
		// 
		if ($_POST['allowcomments'] =='') {
			# code...
			$_POST['allowcomments'] = 0;
		}
		
		$cRe=$newsOb->create();
		if($cRe){
			$re=$newsOb->add();
			if($re){
				$this->success("添加成功。",U("Admin/News/oper"));
			}else{
				$this->error("添加失败",U("Admin/News/add"));
			}
		}else{
			//获取错误信息，提示
			$info=$newsOb->getError();
			$this->error($info,U("Admin/News/add"));
			
		}
		
					
		
	}
	function update(){
			// $id =(int) $_GET['id'];
			// $newsOb = D('News');
			// $arr =$newsOb->where('id = $id')
			// 				->find();
			// $typeOb = D('Type');
			// $optionStr=$newsOb->showOption($arr['typeid']);
			// $this->assign('optionStr',$optionStr);
			// $this->assign('arr',$arr);
			// $this->display();
		$articleid=(int)$_GET['articleid'];
		$newsOb=M("article");
		$arr=$newsOb->where("articleid=$articleid")->find();
	//	$typeOb=D('Type');
		//$optionStr=$typeOb->showOption($arr['typeid']);
		
		$this->assign('optionStr',$optionStr);
		
		$this->assign('arr',$arr);
		
		$this->display();
	}
	function usave(){
		// $type = $_POST['type'];
		// $type = explode('|',$type);
		// $_POST['typefid'] = $type[0];
		// $_POST['typeid'] = $type[1];
		// unset($_POST['type']);
		// $newsOb =D('News');
		// $id =$_POST['id'];
		// $re = $newsOb->where("id=$id")
		// 			->data($_POST)
		// 			->save();
		// if($re){
		// 	$this->success('修改成功',U('Admin/News/oper'));
		// }else{
		// 	$this->error('修改失败',U("Admin/News/update/id/{$id}"));
		// }
		$arr=explode('|',$_POST['type']);
		$_POST['typefid']=$arr[0];
		$_POST['typeid']=$arr[1];
		unset($_POST['type']);
		$newsOb=D("News");
		var_dump($_POST);
		//指定条件
		//产生数据
		//保存
		$re=$newsOb->where("id=$id")
		           ->data($_POST)
		           ->save();
		//提示跳转
		if($re){
			$this->success("修改成功",U("Admin/News/oper"));
		}else{
			$this->error("修改失败",U("Admin/News/oper"));
		}
	}
	function del(){
		$articleid = $_GET['id'];
		$newsOb=D('article');
		$re = $newsOb->where("articleid = $articleid")
					->delete();
		echo $articleid;
	}
	function oper(){
		$newsOb=M('article');
		$arr=$newsOb->order('articleid desc')->select();
		$this->assign('arr',$arr);
		$this->display();
	}
	function xiajia(){
		$articleid= $_GET['id'];
		$articleOb = M('article');
		$arr= array('state'=>0);
		$re = $articleOb->where("articleid=$articleid")
						->data($arr)
						->save();
						echo $articleid;
		
	}
	function shangjia(){
		$articleid= $_GET['id'];
		$articleOb = M('article');
		$arr= array('state'=>1);
		$re = $articleOb->where("articleid=$articleid")
						->data($arr)
						->save();
					echo $articleid;
	}
} 